﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Net;

namespace LicenseData
{
	public class ConversionHelpers
	{
		public const string MySQLDateTimeFormat = "yyyy-MM-dd HH:mm:ss";

		public static DateTimeOffset FromMySQLDate( XElement node )
		{
			return DateTimeOffset.ParseExact( DecodedString( node ), MySQLDateTimeFormat, null,
				System.Globalization.DateTimeStyles.AssumeUniversal );
		}

		public static string ToMySQLDate( DateTimeOffset date )
		{
			return date.ToString( MySQLDateTimeFormat );
		}

		public static string FromBoolean( bool value )
		{
			return value ? "1" : "0";
		}

		public static bool ToBoolean( XElement node )
		{
			return ( string ) node == "1";
		}

		public static string DecodedString( XElement node )
		{
			return WebUtility.HtmlDecode( ( string ) node );
		}
	}
}
